﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Utils.MathUtilitys;
using Utils.Drawing;
using Utils.ContentManagement;

namespace Utils.Drawing
{
    public class PrimitiveHelper
    {
        const int CIRCLE_PARITCLE_COUNT = 16;

        Texture2D texBlank;
        Sprite sprBlank;
        RectSprite sprRect;
        SpriteRenderer renderer;

        public PrimitiveHelper(SpriteRenderer renderer)
        {
            this.renderer = renderer;
        }

        public void Init(ContentLoader loader)
        {
            texBlank = loader.Load<Texture2D>("Textures/blank");
            sprBlank = new Sprite(texBlank);
            sprBlank.Origin = Sprite.ORIGIN_LEFT;

            sprRect = new RectSprite(texBlank);
            sprRect.Origin = Sprite.ORIGIN_TOP_LEFT;
        }

        public void DrawLine(Vector2 p0, Vector2 p1, float width, Color col)
        {
            Vector2 line;
            if (renderer.CoordinateTranslater == null)
            {
                line = p1 - p0;
            }
            else
            {
                line = renderer.CoordinateTranslater.TranslatePosition(p1) 
                        - renderer.CoordinateTranslater.TranslatePosition(p0);
            }

            float angle = Vec2Utils.Vec2ToAngle(line);
            sprBlank.Position = p0;
            sprBlank.Color = col;
            sprBlank.Scale = new Vector2(line.Length(), width) * 0.25f;
            sprBlank.Rotation = angle;

            sprBlank.Draw(0, renderer);
        }

        public void DrawEmptyRect(FRect rect, float width, Color col)
        {
            float left = rect.Left;
            float top = rect.Top;
            float halfWidth = width / 2;
            DrawRect(rect.X, top + halfWidth, rect.Width, width, col);
            DrawRect(left + halfWidth, rect.Y, width, rect.Height - width * 2, col);
            DrawRect(rect.X, top + rect.Height - halfWidth, rect.Width, width, col);
            DrawRect(left + rect.Width - halfWidth, rect.Y, width, rect.Height - width * 2, col);
        }

        public void DrawRect(Rectangle rect, Color col)
        {
            Point center = rect.Center;
            sprRect.Rect = new FRect(center.X, center.Y, rect.Width, rect.Height);
            sprRect.Color = col;
            sprRect.Draw(0, renderer);
        }

        public void DrawRect(FRect rect, Color col)
        {
            sprRect.Rect = rect;
            sprRect.Color = col;            
            sprRect.Draw(0, renderer);
        }

        public void DrawRect(Vector2 pos, Vector2 size, Color col)
        {
            sprRect.Rect = new FRect(pos.X, pos.Y, size.X, size.Y);
            sprRect.Color = col;           
            sprRect.Draw(0, renderer);
        }

        public void DrawRect(float x, float y, float width, float height, Color col)
        {
            sprRect.Rect = new FRect(x, y, width, height);
            sprRect.Color = col;
            sprRect.Draw(0, renderer);
        }

        public void DrawCircle(Vector2 p, float r, float width, Color col)
        {
            Vector2 lastPos = Vector2.Zero;
            for(int i=0; i<=CIRCLE_PARITCLE_COUNT; i++)
            {
                double rad = (float)i / (float)CIRCLE_PARITCLE_COUNT * MathUtils.PI2;
                Vector2 pos = new Vector2(p.X + (float)Math.Sin(rad) * r, p.Y + (float)Math.Cos(rad) * r);

                if(i>0)
                {
                    DrawLine(lastPos, pos, width, col);
                }
                lastPos = pos;
            }
        }
    }
}
